package com.wxapp.dispatch.service.event.listener;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wxapp.common.base.util.bean.SpringUtils;
import com.wxapp.common.constant.base.IBaseListen;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.dispatch.api.pojo.entity.DispatchDO;
import com.wxapp.dispatch.api.pojo.entity.DispatchLogsDO;
import com.wxapp.dispatch.api.pojo.vo.dispatchlogs.QDispatchLogAddVo;
import com.wxapp.dispatch.service.event.DispatchEvent;
import com.wxapp.dispatch.service.mapper.DispatchLogsMapper;
import com.wxapp.dispatch.service.mapper.DispatchMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author lgl
 * @Description
 * @Datetime 2023/8/23 9:42
 * @Modified By
 */
@Component
@RequiredArgsConstructor
public class DispatchListener extends IBaseListen<DispatchEvent, QDispatchLogAddVo> {
    private final DispatchMapper dispatchMapper;
    private final DispatchLogsMapper dispatchLogsMapper;


    @Override
    protected Object toDealListen(DispatchEvent event) {
        var dispatchVm=event.getData();
        var wrapper = new LambdaUpdateWrapper<DispatchDO>();
        wrapper.eq(DispatchDO::getCode, dispatchVm.getDispatchCode()).set(DispatchDO::getLastTime, LocalDateTime.now()).setSql("`counts`=`counts`+1");
        SpringUtils.getBean(DispatchMapper.class).update(null, wrapper);
        if (CheckUtil.isNotNullString(dispatchVm.getLogs())){
            var dispatchLogsDO = CopyUtil.copyObjectNew(dispatchVm, DispatchLogsDO::new, null);
            SpringUtils.getBean(DispatchLogsMapper.class).insert(dispatchLogsDO);
        }
        return null;
    }
}
